package com.b2b.controller;

import com.b2b.exception.CustomException;
import com.b2b.exception.CustomUnauthorizedException;
import com.b2b.model.WalletDto;
import com.b2b.model.common.ResponseBean;
import com.b2b.model.entity.Wallet;
import com.b2b.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/wallet")
@PropertySource("classpath:config.properties")
public class WalletController {
    @Value("${refreshTokenExpireTime}")
    private String refreshTokenExpireTime;

    private final WalletService walletService;


    @Autowired
    public WalletController(WalletService walletService){
        this.walletService = walletService;
    }

    /**
     * 获取钱包
     * @param
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @author dolyw.com
     * @date 2018/8/30 10:41
     */
    @GetMapping("/getMy")
//    @RequiresPermissions(logical = Logical.AND, value = {"user:view"})
    public ResponseBean wallet(WalletDto walletDto) {
        WalletDto walletDtoTemp = new WalletDto();
        if (walletDto.getCompanyId() == null){
            Integer walletId = walletDto.getWalletId();
            walletDtoTemp = walletService.selectByPrimaryKey(walletId);
        }else {
            WalletDto wallet = new WalletDto();
            wallet.setCompanyId(walletDto.getCompanyId());
            walletDtoTemp = walletService.selectOne(wallet);
        }

        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", walletDtoTemp);
    }

    @PutMapping("/updateInfo")
    public ResponseBean updateInfo(@RequestBody WalletDto walletDto){
        if(walletDto.getBalance() == null){
            walletService.updateByPrimaryKeySelective(walletDto);
            return new ResponseBean(HttpStatus.OK.value(), "更新成功(Update Success)", null);
        }
        return new ResponseBean(HttpStatus.FORBIDDEN.value(), "不能修改余额", null);
    }
}
